package com.umeox.moto.mqtt;

import android.content.Context;
import android.util.Log;


import com.umeox.moto.mqtt.base.ARequest;
import com.umeox.moto.mqtt.base.ASend;
import com.umeox.moto.mqtt.base.IOnCallListener;
import com.umeox.moto.mqtt.base.PersistentConnectState;
import com.umeox.moto.mqtt.base.PersistentInitParams;
import com.umeox.moto.mqtt.event.IOnSubscribeListener;
import com.umeox.moto.mqtt.event.IOnSubscribeRpcListener;

import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.MqttException;

public class PersistentNet {
    private static String TAG = "PersistentNet";
    private static String MQTT_PROTOCOL = "MQTT";

    private String defProtocol;
    private IPersisitentNet mqttNet;

    public PersistentNet() {
        this.defProtocol = MQTT_PROTOCOL;
        this.mqttNet = null;
    }

    public static PersistentNet getInstance() {
        return InstanceHolder.INSTANCE;
    }

    public String getDefaultProtocol() {
        return this.defProtocol;
    }

    public void setDefaultProtocol(String protocol) {
        if (protocol != null && protocol.equals(MQTT_PROTOCOL)) {
            defProtocol = MQTT_PROTOCOL;
            mqttNet = MqttNet.getInstance();
        }
    }

    public void init(Context context, PersistentInitParams initParams) throws MqttException {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "init()");
        }
        getPersistentNet().init(context, initParams);
    }

    public void destroy() {
        getPersistentNet().destroy();
    }

    public void disConnectManually(IMqttActionListener callback) {
        getPersistentNet().disConnectManually(callback);
    }


    public PersistentInitParams getInitParams() {
        IPersisitentNet persistentNet = this.getPersistentNet();
        return persistentNet instanceof MqttNet ? ((MqttNet) persistentNet).getInitParams() : null;
    }

    public PersistentConnectState getConnectState() {
        return getPersistentNet().getConnectState();
    }

    public ASend asyncSend(ARequest request, IOnCallListener listener) {
        return getPersistentNet().asyncSend(request, listener);
    }

    public void retry(ASend send) {
        getPersistentNet().retry(send);
    }

    public void subscribe(String topic, IOnSubscribeListener listener) {
        getPersistentNet().subscribe(topic, listener);
    }

    public void unSubscribe(String topic, IOnSubscribeListener listener) {
        getPersistentNet().unSubscribe(topic, listener);
    }

    public void subscribeRpc(String topic, IOnSubscribeRpcListener listener) {
        getPersistentNet().subscribeRpc(topic, listener);
    }

    public void unSubscribeRpc(String topic, IOnSubscribeRpcListener listener) {
        getPersistentNet().unSubscribeRpc(topic, listener);
    }

    private IPersisitentNet getPersistentNet() {
        if (mqttNet == null) {
            mqttNet = MqttNet.getInstance();
        }
        return mqttNet;
    }

    private static class InstanceHolder {
        private static final PersistentNet INSTANCE = new PersistentNet();
    }
}
